import 'package:BeeApp/MainPage/MainToolPage.dart';
import 'package:amap_map_fluttify/amap_map_fluttify.dart';
import 'package:city_pickers/city_pickers.dart';
import 'package:flutter/material.dart';

class UserLocationSearchPage extends StatefulWidget {
  UserLocationSearchPage({Key key}) : super(key: key);

  @override
  _UserLocationSearchPageState createState() => _UserLocationSearchPageState();
}

class _UserLocationSearchPageState extends State<UserLocationSearchPage> {
  final searchText = TextEditingController();
  String cityStr = getData("city");
  List<Poi> addressArr = [];

  @override
  void dispose() {
    super.dispose();
    searchText?.clear();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: PreferredSize(
        preferredSize: Size(0, 0),
        child: AppBar(
          elevation: 0,
        ),
      ),
      body: Column(
        children: [
          topView(),
          Container(
            height: 10,
            color: Color(0xFFF5F5F5F5),
          ),
          Expanded(
            child: addressArr.length == 0
                ? Container()
                : Container(
                    child: ListView.builder(
                      itemCount: addressArr.length,
                      itemBuilder: _listItemBuilder,
                    ),
                  ),
          )
        ],
      ),
    );
  }

  Widget topView() {
    return Container(
      padding: EdgeInsets.fromLTRB(
        setWidth(30),
        setHeight(16),
        setWidth(30),
        setHeight(16),
      ),
      child: Row(
        children: [
          Expanded(
            child: Container(
              height: setHeight(60),
              padding: EdgeInsets.only(left: 10, right: 10),
              decoration: BoxDecoration(
                color: Color(0xFFF2F2F2),
                borderRadius: BorderRadius.all(Radius.circular(20.0)),
              ),
              child: Row(
                children: [
                  InkWell(
                    onTap: () async {
                      FocusScope.of(context).requestFocus(FocusNode());
                      print("object");
                      Result result = await CityPickers.showCityPicker(
                        height: setHeight(600),
                        showType: ShowType.pc,
                        context: context,
                        cancelWidget: Text('取消'),
                        confirmWidget: Text('确认'),
                      );
                      if (result == null) {
                        return;
                      }
                      if (mounted) {
                        setState(() {
                          if (result.provinceId == "110000" ||
                              result.provinceId == "120000" ||
                              result.provinceId == "310000" ||
                              result.provinceId == "500000" ||
                              result.provinceId == "810000" ||
                              result.provinceId == "820000") {
                            cityStr = result.provinceName.toString();
                          } else {
                            cityStr = result.cityName.toString();
                          }
                        });
                      }
                      print(result.toString());
                    },
                    child: Row(
                      children: [
                        Text(
                          cityStr,
                          style: TextStyle(
                              color: Color(0xFF333333),
                              fontSize: setFontSize(30)),
                        ),
                        SizedBox(width: 4),
                        Image.asset(
                          "images/MainMoreNextImage.png",
                          height: setHeight(16),
                        ),
                        SizedBox(width: 8),
                        Text(
                          "|",
                          style: TextStyle(
                              color: Color(0xFF707070),
                              fontSize: setFontSize(30)),
                        ),
                        SizedBox(width: 4),
                      ],
                    ),
                  ),
                  Expanded(
                    child: TextField(
                      controller: searchText,
                      textAlign: TextAlign.start,
                      cursorColor: Colors.grey, //设置光标
                      decoration: InputDecoration(
                        contentPadding: const EdgeInsets.symmetric(
                            vertical: 1.0, horizontal: 1.0),
                        hintText: '请输入地点',
                        hintStyle:
                            TextStyle(fontSize: 14, color: Color(0xFFAEAEAE)),
                        border: OutlineInputBorder(
                            borderRadius: BorderRadius.circular(15),
                            borderSide: BorderSide.none),
                        fillColor: Color(0xFFF2F2F2),
                      ),
                      style: TextStyle(fontSize: 14, color: Colors.black),
                      onChanged: (value) async {
                        if (value.length == 0) {
                          return;
                        }
                        List<Poi> arr = await AmapSearch.instance
                            .searchKeyword(value, city: cityStr);
                        if (arr.length > 0) {
                          addressArr?.clear();
                          if (mounted) {
                            setState(() {
                              for (var i = 0; i < arr.length; i++) {
                                addressArr.add(arr[i]);
                                print(arr[i].provinceName);
                                print(arr[i].cityName);
                                print(arr[i].adName);
                                print(arr[i].address);
                                print(arr[i].title);
                                print(arr[i].latLng.latitude);
                                print(arr[i].latLng.longitude);
                                print(arr[i].distance);
                              }
                            });
                          }
                        }
                      },
                    ),
                  ),
                ],
              ),
            ),
          ),
          SizedBox(width: 8),
          InkWell(
            child: Text("取消"),
            onTap: () {
              FocusScope.of(context).requestFocus(FocusNode());
              Navigator.pop(context);
            },
          ),
        ],
      ),
    );
  }

  Widget _listItemBuilder(BuildContext context, int index) {
    return InkWell(
      onTap: () async {
        await removeFocus(context);
        Navigator.pop(context, addressArr[index]);
      },
      child: Container(
        padding: EdgeInsets.fromLTRB(setWidth(30), 0, 0, 0),
        child: Container(
          child: Column(
            children: [
              Container(
                padding: EdgeInsets.fromLTRB(
                  0,
                  setHeight(40),
                  setWidth(30),
                  setHeight(40),
                ),
                child: Row(
                  children: [
                    Image.asset(
                      "images/MainLocationImage.png",
                      height: setHeight(50),
                    ),
                    Expanded(
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Text(
                            addressArr[index].title,
                            style: TextStyle(
                              fontSize: setFontSize(30),
                              color: Color(0xFF333333),
                            ),
                          ),
                          Text(
                            addressArr[index].provinceName +
                                addressArr[index].cityName +
                                addressArr[index].adName +
                                addressArr[index].address,
                            style: TextStyle(
                              fontSize: setFontSize(24),
                              color: Color(0xFF666666),
                            ),
                          ),
                        ],
                      ),
                    ),
                    // Text("data",
                    //     style: TextStyle(
                    //       fontSize: setFontSize(24),
                    //       color: Color(0xFF666666),
                    //     )),
                  ],
                ),
              ),
              Container(
                color: Color(0xFFEDEDED),
                height: 1,
              ),
            ],
          ),
        ),
      ),
    );
  }
}
